package 力扣日常刷题.木22一月.第12天0120;

/**
 * @author 帅小伙
 * @date 2022/1/20
 * @description
 * 打家劫舍一样
 * 打家劫舍不能连续偷两家
 */
public class Demo32动态规划中等删除获得点数 {


    public int deleteAndEarn(int[] nums) {
        int maxVal = Integer.MIN_VALUE;
        for (int num : nums) {
            maxVal = Math.max(maxVal,num);
        }
        int[]sum = new int[maxVal+1];
        for (int num : nums) {
            sum[num] += num;
        }
        return rob(sum);
    }


    int rob(int[] sum){
        int size = sum.length;

        int first = sum[0] , second   = Math.max(sum[0],sum[1]);

        for (int i = 2; i < size; i++) {
            int t = second;
            second = Math.max(first+sum[i],second);
            first = t;
        }
        return second;
    }
}
